var Mail = {
	Send: {
		applyWindowContainer: '',
		addItemWindowContainer: '',
		infoWindowContainer: '',
		
		init: function() {
			Language.getCurrLanguage();
			Utils.loadJavascriptFile('/application/views/libraries/component/' + language + '.js');
			Utils.loadJavascriptFile('/application/views/libraries/utils/' + language + '.js');
			Utils.loadJavascriptFile('/application/views/module/Mail/' + language + '.js');
			Utils.initAjax();
		
			Mail.Send.applyWindowContainer = $('#applyWindow');
			Mail.Send.ApplyWindow.init();
			Mail.Send.addItemWindowContainer = $('#addItemWindow');
			Mail.Send.AddItemWindow.init();
			Mail.Send.infoWindowContainer = $('#infoWindow');
			Mail.Send.InfoWindow.init();
			Mail.Send.View.init();
		},
		
		View: {
			dataGridPanelContainer: '',
			
			init: function() {
				Mail.Send.View.dataGridPanelContainer = $('#dataGridPanel #dataGrid');
				Mail.Send.View.DataGrid.init();
			},
			
			DataGrid: {
				init: function() {
					Mail.Send.View.DataGrid.create();
				},
				
				create: function() {
					var width = $(document).width() - 40;
					var height = $(document).height() - 40;
					Mail.Send.View.dataGridPanelContainer.width(width);
					Mail.Send.View.dataGridPanelContainer.height(height);
					Mail.Send.View.dataGridPanelContainer.datagrid({
						idField: "applyId",
						url: "/index.php/Mail/send/getListData",
						singleSelect: true,
						toolbar: [
							{
								iconCls: 'icon-add',
								text: mailSendViewDataGridToolbarButtonText1,
								handler: Mail.Send.ApplyWindow.open,
							},
							{
								iconCls: 'icon-edit',
								text: mailSendViewDataGridToolbarButtonText2,
								handler: Mail.Send.View.doApproval,
							},
							{
								iconCls: 'icon-search',
								text: mailSendViewDataGridToolbarButtonText3,
								handler: Mail.Send.InfoWindow.open,
							}
						],
						columns: [[
							{
								checkbox: true,
							},
							{
								field: 'applyId',
								title: mailSendViewDataGridColumnTitle1,
								width: 60,
							},
							{
								field: 'roleName',
								title: mailSendViewDataGridColumnTitle3,
								width: 100,
							},
							{
								field: 'roleLevel',
								title: mailSendViewDataGridColumnTitle4,
								width: 100,
							},
							{
								field: 'senderName',
								title: mailSendViewDataGridColumnTitle6,
								width: 80,
							},
							{
								field: 'title',
								title: mailSendViewDataGridColumnTitle7,
								width: 100,
							},
							{
								field: 'content',
								title: mailSendViewDataGridColumnTitle8,
								width: 200,
							},
							{
								field: 'item',
								title: mailSendViewDataGridColumnTitle15,
								width: 150,
							},
							{
								field: 'status',
								title: mailSendViewDataGridColumnTitle9,
								width: 80,
								formatter: function(value) {
									if(value === '1') {
										return mailSendViewApplyStatus1;
									}
									else if(value === '2') {
										return mailSendViewApplyStatus2;
									}
									else if(value === '3') {
										return mailSendViewApplyStatus3;
									}
									else if(value === '4') {
										return mailSendViewApplyStatus4;
									}
									else {
										return value;
									}
								}
							},
							{
								field: 'applyUserName',
								title: mailSendViewDataGridColumnTitle10,
								width: 80,
							},
							{
								field: 'applyDatetime',
								title: mailSendViewDataGridColumnTitle11,
								width: 150,
							},
							{
								field: 'approvalUserName',
								title: mailSendViewDataGridColumnTitle12,
								width: 80,
							},
							{
								field: 'approvalDatetime',
								title: mailSendViewDataGridColumnTitle13,
								width: 150,
							},
							
						]],
						loadFilter: Utils.dataGridLoadFilter,
						pagination: true,
						pageSize: 50,
						pageList: [50, 100, 150, 200],
					});
				},
				
				load: function() {
					Mail.Send.View.dataGridPanelContainer.datagrid('reload');
				},
				
				getCheckedRows: function(allowMulti) {
					var allowMulti = arguments[0] ? arguments[0] : false;
					var checkedRow = Mail.Send.View.dataGridPanelContainer.datagrid('getChecked');
					
					if(Utils.dataGridValidateCheckedRow(checkedRow, allowMulti) === true) {
						return checkedRow;
					}
					else {
						return false;
					}
				},
			},
			
			doApproval: function() {
				var checkedRow = Mail.Send.View.DataGrid.getCheckedRows();
				
				if(checkedRow != false) {
					$.messager.defaults = {ok: mailSendApprovalConfirmWindowOkMessage, cancel: mailSendApprovalConfirmWindowCancelMessage};
					$.messager.confirm(
						'Confirm',
						mailSendApprovalConfirmWindowMessage,
						function(r){
							$.messager.defaults = {ok: 'Ok', cancel: 'Cancel'};
							
							if(r) {
								status = 2;
							}
							else {
								status = 3;
							}
							
							$.ajax({
								url: '/index.php/Mail/send/approval',
								data: {
									applyId: checkedRow[0].applyId,
									status: status,
								}
							})
							.done(function(response) {
								if(response.errorCode === 0) {
									$.messager.alert(messageWindowTitle, mailSendApprovalSuccessInfo, 'info');
									Mail.Send.View.DataGrid.load();
								}
							})
						}
					)				
				}
			},
		},
		
		ApplyWindow: {
			gameAreaCombogridLabelContainer: '',
			gameAreaCombogridInputContainer: '',
			roleNameValidateboxLabelContainer: '',
			roleNameValidateboxInputContainer: '',
			roleLevelRangeLabelContainer: '',
			minRoleLevelValidateboxInputContainer: '',
			maxRoleLevelValidateboxInputContainer: '',
			senderNameValidateboxLabelContainer: '',
			senderNameValidateboxInputContainer: '',
			titleValidateboxLabelContainer: '',
			titleValidateboxInputContainer: '',
			contentTextboxLabelContainer: '',
			contentTextboxInputContainer: '',
			submitButtonContainer: '',
			resetButtonContainer: '',
			itemDataGridContainer: '',
			
			init: function() {
				Mail.Send.ApplyWindow.gameAreaCombogridLabelContainer = $('#applyWindow #gameAreaCombogridLabel');
				Mail.Send.ApplyWindow.gameAreaCombogridInputContainer = $('#applyWindow #gameAreaCombogridInput');
				Mail.Send.ApplyWindow.roleNameValidateboxLabelContainer = $('#applyWindow #roleNameValidateboxLabel');
				Mail.Send.ApplyWindow.roleNameValidateboxInputContainer = $('#applyWindow #roleNameValidateboxInput');
				Mail.Send.ApplyWindow.roleLevelRangeLabelContainer = $('#applyWindow #roleLevelRangeLabel');
				Mail.Send.ApplyWindow.minRoleLevelValidateboxInputContainer = $('#applyWindow #minRoleLevelValidateboxInput');
				Mail.Send.ApplyWindow.maxRoleLevelValidateboxInputContainer = $('#applyWindow #maxRoleLevelValidateboxInput');
				Mail.Send.ApplyWindow.senderNameValidateboxLabelContainer = $('#applyWindow #senderNameValidateboxLabel');
				Mail.Send.ApplyWindow.senderNameValidateboxInputContainer = $('#applyWindow #senderNameValidateboxInput');
				Mail.Send.ApplyWindow.titleValidateboxLabelContainer = $('#applyWindow #titleValidateboxLabel');
				Mail.Send.ApplyWindow.titleValidateboxInputContainer = $('#applyWindow #titleValidateboxInput');
				Mail.Send.ApplyWindow.contentTextboxLabelContainer = $('#applyWindow #contentTextboxLabel');
				Mail.Send.ApplyWindow.contentTextboxInputContainer = $('#applyWindow #contentTextboxInput');
				Mail.Send.ApplyWindow.itemDataGridContainer = $('#applyWindow #itemDataGrid');
				Mail.Send.ApplyWindow.submitButtonContainer = $('#applyWindow #submitButton');
				Mail.Send.ApplyWindow.resetButtonContainer = $('#applyWindow #resetButton');
				Mail.Send.ApplyWindow.create();
				Mail.Send.ApplyWindow.ItemDataGrid.create();
			},
			
			ItemDataGrid: {
				create: function() {
					Mail.Send.ApplyWindow.itemDataGridContainer.datagrid({
						width: 248,
						height: 403,
						fitColumns: false,
						border: false,
						toolbar: [
							{
								iconCls: 'icon-add',
								text: mailSendApplyWindowItemDataGridToolbarButtonText1,
								handler: Mail.Send.AddItemWindow.open,
							},
							{
								iconCls: 'icon-remove',
								text: mailSendApplyWindowItemDataGridToolbarButtonText2,
								handler: Mail.Send.ApplyWindow.ItemDataGrid.deleteRow,
							},
						],
						columns: [[
							{
								checkbox: true
							},
							{
								field: 'itemName',
								title: mailSendApplyWindowItemDataGridColumnTitle1,
								width: 100
							},
							{
								field: 'count',
								title: mailSendApplyWindowItemDataGridColumnTitle2,
								width: 100
							},
						]],
					})
				},
				
				appendRow: function(id, itemName, count) {
					Mail.Send.ApplyWindow.itemDataGridContainer.datagrid(
						'appendRow',
						{
							id: id,
							itemName: itemName,
							count: count
						}
					);
				},
				
				deleteRow: function() {
					var checkedRow = Mail.Send.ApplyWindow.ItemDataGrid.getCheckedRows(true);
					
					$.each(
							checkedRow,
						function(idx, val) {
							var index = Mail.Send.ApplyWindow.itemDataGridContainer.datagrid('getRowIndex', val);
							Mail.Send.ApplyWindow.itemDataGridContainer.datagrid('deleteRow', index);
						}
					);
				},
				
				getCheckedRows: function(allowMulti) {
					var allowMulti = arguments[0] ? arguments[0] : false;
					var checkedRow = Mail.Send.ApplyWindow.itemDataGridContainer.datagrid('getChecked');
					
					if(Utils.dataGridValidateCheckedRow(checkedRow, allowMulti) === true) {
						return checkedRow;
					}
					else {
						return false;
					}
				},
				
				reset: function() {
					Mail.Send.ApplyWindow.itemDataGridContainer.datagrid(
						'loadData',
						[]
					);
				}
			},
			
			create: function() {
				Mail.Send.applyWindowContainer.window({
					title: mailSendApplyWindowTitle,
					width: 533,
					height: 500,
					collapsible: false,
					minimizable: false,
					maximizable: false,
					closable: true,
					closed: true,
					modal: true,
				});
				Component.Combogrid.GameArea.create(
					Mail.Send.ApplyWindow.gameAreaCombogridLabelContainer,
					Mail.Send.ApplyWindow.gameAreaCombogridInputContainer,
					true
				);
				Component.Validatebox.RoleName.create(
					Mail.Send.ApplyWindow.roleNameValidateboxLabelContainer,
					Mail.Send.ApplyWindow.roleNameValidateboxInputContainer
				);
				Component.Range.RoleLevel.create(
					Mail.Send.ApplyWindow.roleLevelRangeLabelContainer,
					Mail.Send.ApplyWindow.minRoleLevelValidateboxInputContainer,
					Mail.Send.ApplyWindow.maxRoleLevelValidateboxInputContainer
				);
				Component.Validatebox.SenderName.create(
					Mail.Send.ApplyWindow.senderNameValidateboxLabelContainer,
					Mail.Send.ApplyWindow.senderNameValidateboxInputContainer
				);
				Component.Validatebox.Title.create(
					Mail.Send.ApplyWindow.titleValidateboxLabelContainer,
					Mail.Send.ApplyWindow.titleValidateboxInputContainer
				);
				Component.Textbox.Content.create(
					Mail.Send.ApplyWindow.contentTextboxLabelContainer
				);
				Mail.Send.ApplyWindow.submitButtonContainer.linkbutton({
					iconCls: 'icon-ok',
					text: mailSendApplyWindowSubmitButtonText,
				});
				Mail.Send.ApplyWindow.submitButtonContainer.bind(
					'click',
					Mail.Send.ApplyWindow.doSubmit
				);
				Mail.Send.ApplyWindow.resetButtonContainer.linkbutton({
					iconCls: 'icon-undo',
					text: mailSendApplyWindowResetButtonText,
				})
				Mail.Send.ApplyWindow.resetButtonContainer.bind(
					'click',
					Mail.Send.ApplyWindow.doReset
				);
			},
			
			open: function() {
				Mail.Send.ApplyWindow.reset();
				Mail.Send.applyWindowContainer.window('open');
			},
			
			close: function() {
				Mail.Send.applyWindowContainer.window('close');
			},
			
			doSubmit: function() {
				var gameAreaIdArray = Mail.Send.ApplyWindow.gameAreaCombogridInputContainer.combogrid('getValues');
				var roleName = Mail.Send.ApplyWindow.roleNameValidateboxInputContainer[0].value;
				var minRoleLevel = Mail.Send.ApplyWindow.minRoleLevelValidateboxInputContainer[0].value;
				var maxRoleLevel = Mail.Send.ApplyWindow.maxRoleLevelValidateboxInputContainer[0].value;
				var senderName = Mail.Send.ApplyWindow.senderNameValidateboxInputContainer[0].value;
				var title = Mail.Send.ApplyWindow.titleValidateboxInputContainer[0].value;
				var content = Mail.Send.ApplyWindow.contentTextboxInputContainer[0].value;
				var itemArray = Mail.Send.ApplyWindow.itemDataGridContainer.datagrid('getRows');
				
				var gameAreaIdString
				
				if(gameAreaIdArray.length > 0) {
					gameAreaIdString = gameAreaIdArray.toString();
				}
				
				var itemString
				if(itemArray.length > 0) {
					var itemString = '[';
					$.each(
						itemArray,
						function(idx, val) {
							itemString = itemString + '{"id":' + val['id'] + ', "count":' + val['count'] +'},'
						}
					);
					
					itemString = itemString.substring(0, itemString.length-1) + ']';
				}
				
				$.ajax({
					url: '/index.php/Mail/send/apply',
					data: {
						gameAreaIdString: gameAreaIdString,
						roleName: roleName,
						minRoleLevel: minRoleLevel,
						maxRoleLevel: maxRoleLevel,
						senderName: senderName,
						title: title,
						content: content,
						itemString: itemString,
					}
				})
				.done(function(response) {
					if(response.errorCode === 0) {
						$.messager.alert(messageWindowTitle, mailSendApplyWindowSubmitSuccessInfo1, 'info');
						Mail.Send.ApplyWindow.close();
						Mail.Send.View.DataGrid.load();
					}
				})
			},
			
			doReset: function() {
				Mail.Send.ApplyWindow.reset();
			},
			
			reset: function() {
				Mail.Send.ApplyWindow.gameAreaCombogridInputContainer.combogrid('reset');
				Mail.Send.ApplyWindow.roleNameValidateboxInputContainer[0].value = '';
				Mail.Send.ApplyWindow.minRoleLevelValidateboxInputContainer[0].value = '';
				Mail.Send.ApplyWindow.maxRoleLevelValidateboxInputContainer[0].value = '';
				Mail.Send.ApplyWindow.senderNameValidateboxInputContainer[0].value = '';
				Mail.Send.ApplyWindow.titleValidateboxInputContainer[0].value = '';
				Mail.Send.ApplyWindow.contentTextboxInputContainer[0].value = '';
				Mail.Send.ApplyWindow.ItemDataGrid.reset();
			},
		},
		
		AddItemWindow: {
			itemComboboxLabelContainer: '',
			itemComboboxInputContainer: '',
			countNumberboxLabelContainer: '',
			countNumberboxInputContainer: '',
			submitButtonContainer: '',
			resetButtonContainer: '',
			
			init: function() {
				Mail.Send.AddItemWindow.itemComboboxLabelContainer = $('#addItemWindow #itemComboboxLabel');
				Mail.Send.AddItemWindow.itemComboboxInputContainer = $('#addItemWindow #itemComboboxInput');
				Mail.Send.AddItemWindow.countNumberboxLabelContainer = $('#addItemWindow #countNumberboxLabel');
				Mail.Send.AddItemWindow.countNumberboxInputContainer = $('#addItemWindow #countNumberboxInput');
				Mail.Send.AddItemWindow.submitButtonContainer = $('#addItemWindow #submitButton');
				Mail.Send.AddItemWindow.resetButtonContainer = $('#addItemWindow #resetButton');
				Mail.Send.AddItemWindow.create();
			},
			
			create: function() {
				Mail.Send.addItemWindowContainer.window({
					title: mailSendAddItemWindowTitle,
					collapsible: false,
					minimizable: false,
					maximizable: false,
					closable: true,
					closed: true,
					modal: true,
				});
				Component.Combobox.Item.create(
					Mail.Send.AddItemWindow.itemComboboxLabelContainer,
					Mail.Send.AddItemWindow.itemComboboxInputContainer
				);
				Component.Numberbox.Count.create(
					Mail.Send.AddItemWindow.countNumberboxLabelContainer,
					Mail.Send.AddItemWindow.countNumberboxInputContainer
				);
				Mail.Send.AddItemWindow.submitButtonContainer.linkbutton({
					iconCls: 'icon-ok',
					text: mailSendAddItemWindowSubmitButtonText,
				});
				Mail.Send.AddItemWindow.submitButtonContainer.bind(
					'click',
					Mail.Send.AddItemWindow.doSubmit
				);
				Mail.Send.AddItemWindow.resetButtonContainer.linkbutton({
					iconCls: 'icon-undo',
					text: mailSendAddItemWindowResetButtonText,
				})
				Mail.Send.AddItemWindow.resetButtonContainer.bind(
					'click',
					Mail.Send.AddItemWindow.doReset
				);
			},
			
			open: function() {
				Mail.Send.AddItemWindow.reset();
				Mail.Send.addItemWindowContainer.window('open');
			},
			
			close: function() {
				Mail.Send.addItemWindowContainer.window('close');
			},
			
			doSubmit: function() {
				var id = Mail.Send.AddItemWindow.itemComboboxInputContainer.combobox('getValue');
				var itemName = Mail.Send.AddItemWindow.itemComboboxInputContainer.combobox('getText');
				var itemCount = Mail.Send.AddItemWindow.countNumberboxInputContainer.numberbox('getValue');
				
				if(id <= 0) {
					$.messager.alert(messageWindowTitle, mailSendApplyWindowSubmitErrorInfo1, 'info');
				}
				else if(itemCount <= 0) {
					$.messager.alert(messageWindowTitle, mailSendApplyWindowSubmitErrorInfo2, 'info');
				}
				else {
					Mail.Send.ApplyWindow.ItemDataGrid.appendRow(id, itemName, itemCount);
					Mail.Send.AddItemWindow.close();
				}
			},
			
			doReset: function() {
				Mail.Send.AddItemWindow.reset();
			},
			
			reset: function() {
				Mail.Send.AddItemWindow.itemComboboxInputContainer.combobox('reset');
				Mail.Send.AddItemWindow.countNumberboxInputContainer.numberbox('clear');
			},
		},
		
		InfoWindow: {
			dataGridContainer: '',
			
			init: function() {
				Mail.Send.InfoWindow.dataGridContainer = $('#infoWindow #dataGrid');
				Mail.Send.InfoWindow.DataGrid.init();
				Mail.Send.InfoWindow.create();
			},
			
			DataGrid: {
				init: function() {
					Mail.Send.InfoWindow.DataGrid.create();
				},
				
				create: function() {
					Mail.Send.InfoWindow.dataGridContainer.datagrid({
						width: 547,
						height: 392,
						fitColumns: true,
						border: false,
						columns: [[
							{
								field: 'operatorName',
								title: MailSendInfoWindowDataGridColumnTitle1,
								width: 100,
							},
							{
								field: 'areaNum',
								title: MailSendInfoWindowDataGridColumnTitle2,
								width: 80,
							},
							{
								field: 'status',
								title: MailSendInfoWindowDataGridColumnTitle3,
								width: 100,
								formatter: function(value) {
									if(value === '1') {
										return mailSendInfoWindowSendStatus1;
									}
									else if(value === '2') {
										return mailSendInfoWindowSendStatus2;
									}
									else {
										return value;
									}
								}
							},
						]],
						loadFilter: Utils.dataGridLoadFilter,
						pagination: true,
						pageSize: 50,
						pageList: [50, 100, 150, 200],
					})
				},
			},
			
			create: function() {
				Mail.Send.infoWindowContainer.window({
					width: 600,
					height: 450,
					title: mailSendInfoWindowTitle,
					collapsible: false,
					minimizable: false,
					maximizable: false,
					closable: true,
					closed: true,
					modal: true,
				});
			},
			
			open: function() {
				var checkedRow = Mail.Send.View.DataGrid.getCheckedRows();
				
				if(checkedRow != false) {
					Mail.Send.InfoWindow.dataGridContainer.datagrid({
						url: '/index.php/Mail/send/getInfoData',
						queryParams: {
							applyId: checkedRow[0].applyId,
						}
					})
					Mail.Send.infoWindowContainer.window('open');
				}
			},
		},
	},
}